extends Panel

signal panel_closed

var inventory: Inventory

func _ready():
	visible = false

func show_panel(inv: Inventory):
	inventory = inv
	update_display()
	visible = true

func hide_panel():
	visible = false
	panel_closed.emit()

func update_display():
	if not inventory:
		return
	
	var item_list = $VBoxContainer/ScrollContainer/ItemList
	
	# 清除现有项目
	for child in item_list.get_children():
		child.queue_free()
	
	# 添加物品
	var items = inventory.get_items()
	var has_items = false
	
	for item in items:
		if item.id != "" and item.count > 0:
			has_items = true
			var item_container = HBoxContainer.new()
			
			var name_label = Label.new()
			name_label.text = inventory.get_item_name(item.id)
			name_label.size_flags_horizontal = Control.SIZE_EXPAND_FILL
			
			var count_label = Label.new()
			count_label.text = "x%d" % item.count
			count_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_RIGHT
			
			item_container.add_child(name_label)
			item_container.add_child(count_label)
			item_list.add_child(item_container)
	
	if not has_items:
		var empty_label = Label.new()
		empty_label.text = "背包为空"
		empty_label.horizontal_alignment = HORIZONTAL_ALIGNMENT_CENTER
		item_list.add_child(empty_label)

func _on_close_button_pressed():
	hide_panel()